/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation
**
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

/*!   
 *    Description: Audio player engine. 
 */

#include "audioplayer_p.h"
#include "audioengine.h"

_LIT(KAudioFileWMA, "c:\\Sounds\\newstories.wma");

/*!
 * Create the instance of the audio player engine.
 */
CAudioPlayerEngine* CAudioPlayerEngine::NewL(AudioPlayerPrivate* audioPlayerPvt)
    {
    CAudioPlayerEngine* self = CAudioPlayerEngine::NewLC(audioPlayerPvt);
    CleanupStack::Pop(self);
    return self;
    }

/*!
 * Creates the instance of the audio player engine.
 */
CAudioPlayerEngine* CAudioPlayerEngine::NewLC(AudioPlayerPrivate* audioPlayerPvt)
    {
    CAudioPlayerEngine* self = new (ELeave) CAudioPlayerEngine(audioPlayerPvt);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

/*!
 * Initialize the member data.
 */
CAudioPlayerEngine::CAudioPlayerEngine(AudioPlayerPrivate* audioPlayerPvt):iAudioFileOpen(EFalse), iAudioPlaying(EFalse),
                                                                           iAudioPlayerPvt(audioPlayerPvt)
  {
  }

/*!
 * Delete the player instance and close the file.
 */
CAudioPlayerEngine::~CAudioPlayerEngine()
  {    
    if(iAudioFileOpen)
        {
        iPlayer->Close();
        }
    delete iPlayer;
  }

/*!
 * The second phase constructor constructs pointer data member of CMdaAudioPlayerUtility
 * and opens the file.
 */
void CAudioPlayerEngine::ConstructL()
    {
    iPlayer = CMdaAudioPlayerUtility::NewL(*this);   
    iPlayer = CMdaAudioPlayerUtility::NewFilePlayerL( KAudioFileWMA, *this );
    }


/*!
 * This function is called when a file is opened by the player.
 * i.e when CMdaAudioPlayerUtility::OpenFileL is called.
 * Try to play the file, only when it is successfully opened.
 */
void CAudioPlayerEngine::MapcInitComplete(TInt aError, 
                               const TTimeIntervalMicroSeconds &/*aDuration*/)
   {
    if(aError == KErrNone)
        {        
        iAudioFileOpen = ETrue;        
        }
    else
        {        
        iAudioFileOpen = EFalse;        
        }
   }


/*!
 * This function is called, when playing of the file completes.
 */
void CAudioPlayerEngine::MapcPlayComplete(TInt aError)
  {  
  }


/*!
 * This method is called to play the opened audio file.
 * After playing of the file completes, MapcPlayComplete() callback function is called.
 */
void CAudioPlayerEngine::Play()
    {
    // play the audio file, if the file is opened successfully.
    if(iAudioFileOpen)
        {
        iPlayer->Play();
        _LIT(KSuccess,"Playing New Stories");
        iResponse = KSuccess;
        iAudioPlaying = ETrue;
        iAudioPlayerPvt->printResponse();
        }
    else
        {
        _LIT(KFailure,"Playing song failed");
        iResponse = KFailure;
        iAudioPlayerPvt->printResponse();
        }
    }


/*!
 * Stop the audio file which is playing.
 */
void CAudioPlayerEngine::Stop()
    {
    // We can stop the audio file, only when it is playing.
    if(iAudioPlaying)
        {
        iPlayer->Stop();
        _LIT(KSuccess,"New Stories Stopped");
        iResponse = KSuccess;
        iAudioPlaying = EFalse;
        iAudioPlayerPvt->printResponse();
        }
    else
        {
        _LIT(KFailure,"Unable to stop");
        iResponse = KFailure;
        iAudioPlayerPvt->printResponse();
        }    
    }


/*!
 * Pause the audio file which is playing.
 */
void CAudioPlayerEngine::Pause()
    {
    // We can pause the audio file, only when it is playing.
    if(iAudioPlaying)
        {
        iPlayer->Pause();
        _LIT(KSuccess,"Pausing New Stories");
        iResponse = KSuccess;
        iAudioPlaying = EFalse;
        iAudioPlayerPvt->printResponse();
        }
    else
        {
        _LIT(KFailure,"Unable to pause the song");
        iResponse = KFailure;
        iAudioPlayerPvt->printResponse();
        }
    }

// END OF FILE

